﻿using CoreTools.DTO.TestDto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;

namespace CoreTools.Controllers
{
    /// <summary>
    /// Home 控制器
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        /// <summary>
        /// 用户列表
        /// </summary>
        public static IList<VUserInfoDto> ListUser = new List<VUserInfoDto>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public HomeController()
        {

        }

        /// <summary>
        ///  获取用户的信息
        /// </summary>
        /// <param name="Id">用户ID</param>
        /// <returns></returns>
        [HttpGet("GetPersonInfo")]
        public async Task<ObjectResult> GetPersonInfo(int Id)
        {
            var userInfo = ListUser.FirstOrDefault(t => t.Id == Id);

            return new ObjectResult(userInfo);
        }

       /// <summary>
       /// 设置用户的信息
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
        [HttpPost("AddPersonInfo")]
        public async Task<ObjectResult> AddPersonInfo(VUserInfoDto model)
        {

            if (ModelState.IsValid)
            {
                ListUser.Add(model);
            }

            return new ObjectResult($"成功:{ModelState.IsValid}");
        }

        /// <summary>
        ///  获取用户集合
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <returns></returns>
        [HttpGet("PersonInfoList")]
        public async Task<ObjectResult> PersonInfoList(int pageIndex,int pageSize)
        {
            var listUser=ListUser.OrderByDescending(t => t.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return new ObjectResult(listUser);
        }

    }
}
